<template>
	<view>
		<u-card :show-head="false" padding="30" margin="20rpx" :foot-border-top="false">
			<view class="card-body" slot="body">
				<u-form :model="form" ref="uForm" label-width="180">
					<view class="title">
						{{type==='add'?'报价':'修改报价'}}
					</view>
					<u-form-item label="报价:" :border-bottom="false" prop="quotedPrice" v-if="workflowId === '1'">
						<u-input v-model="form.quotedPrice" border type="digit" />
					</u-form-item>
					<view v-else-if="workflowId === '2'">
						<u-form-item label="≤1吨:" :border-bottom="false" prop="quotedPrice1">
							<u-input v-model="form.quotedPrice1" border type="digit" />
						</u-form-item>
						<u-form-item label="≤3吨:" :border-bottom="false" prop="quotedPrice2">
							<u-input v-model="form.quotedPrice2" border type="digit" />
						</u-form-item>
						<u-form-item label="≤5吨:" :border-bottom="false" prop="quotedPrice3">
							<u-input v-model="form.quotedPrice3" border type="digit" />
						</u-form-item>
						<u-form-item label="≤10吨:" :border-bottom="false" prop="quotedPrice4">
							<u-input v-model="form.quotedPrice4" border type="digit" />
						</u-form-item>
						<u-form-item label="≤15吨:" :border-bottom="false" prop="quotedPrice5">
							<u-input v-model="form.quotedPrice5" border type="digit" />
						</u-form-item>
						<u-form-item label="≤20吨:" :border-bottom="false" prop="quotedPrice6">
							<u-input v-model="form.quotedPrice6" border type="digit" />
						</u-form-item>
						<u-form-item label="≤25吨:" :border-bottom="false" prop="quotedPrice7">
							<u-input v-model="form.quotedPrice7" border type="digit" />
						</u-form-item>
						<u-form-item label="≤30吨:" :border-bottom="false" prop="quotedPrice8">
							<u-input v-model="form.quotedPrice8" border type="digit" />
						</u-form-item>
					</view>
					<view v-else-if="workflowId === '3'">
						<u-form-item label="船公司" :border-bottom="false" prop="shippingCompany">
							<u-input v-model="form.shippingCompany" border />
						</u-form-item>
						<u-form-item label="海运费:" :border-bottom="false" prop="shippingFee">
							<u-input v-model="form.shippingFee" border type="digit" />
						</u-form-item>
						<u-form-item label="港杂费:" :border-bottom="false" prop="portCharges">
							<u-input v-model="form.portCharges" border type="digit" />
						</u-form-item>
						<u-form-item label="船期_ETD:" :border-bottom="false" prop="shippingDateETD">
							<u-input v-model="form.shippingDateETD" border />
						</u-form-item>
						<u-form-item label="船期_ETA:" :border-bottom="false" prop="scheduleEta">
							<u-input v-model="form.scheduleEta" border />
						</u-form-item>
						<u-form-item label="起运港(箱使/天):" :border-bottom="false" prop="departurePortBoxAngel">
							<u-input v-model="form.departurePortBoxAngel" border />
						</u-form-item>
						<u-form-item label="目的港(箱使/天):" :border-bottom="false" prop="destinationPortBoxAngel">
							<u-input v-model="form.destinationPortBoxAngel" border />
						</u-form-item>
						<u-form-item label="是否直达:" :border-bottom="false" prop="isItDirect">
							<u-input v-model="form.isItDirect" border />
						</u-form-item>
					</view>
					<u-form-item label="报价说明:" :border-bottom="false">
						<u-input v-model="form.quoteDescription" border />
					</u-form-item>
					<u-form-item label="备注:" :border-bottom="false" v-if="workflowId !== '3'">
						<u-input v-model="form.remark" border type="textarea" />
					</u-form-item>
				</u-form>
			</view>
			<view class="u-footer" slot="foot">
				<u-button plain size="medium" @click="back">返回</u-button>
				<u-button type="primary" size="medium" @click="submit()">保存</u-button>
			</view>
		</u-card>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					quotedPrice: '', //报价
					quoteDescription: '', //报价说明
					remark: '', //备注
					quotedPrice1: '', //≤1吨
					quotedPrice2: '', //≤3吨
					quotedPrice3: '', //≤5吨
					quotedPrice4: '', //≤10吨
					quotedPrice5: '', //≤15吨
					quotedPrice6: '', //≤20吨
					quotedPrice7: '', //≤25吨
					quotedPrice8: '', //≤30吨
					id: '', //内贸提交时的id
					shippingCompany: '', //船公司
					shippingFee: '', //海运费
					portCharges: '', //港杂费
					shippingDateETD: '', //船期_ETD
					scheduleEta: '', //船期_ETA
					departurePortBoxAngel: '', //起运港(箱使/天)
					destinationPortBoxAngel: '', //目的港(箱使/天)
					isItDirect: '', //是否直达

				},
				rules: {
					quotedPrice: [{
						required: true,
						message: '请输入价格',
						trigger: 'blur,change'
					}],

				},
				//内贸散货校验
				bulkCargoRules: {
					quotedPrice1: [{
						required: true,
						message: '请输入价格',
						trigger: 'blur,change'
					}],
					quotedPrice2: [{
						required: true,
						message: '请输入价格',
						trigger: 'blur,change'
					}],
					quotedPrice3: [{
						required: true,
						message: '请输入价格',
						trigger: 'blur,change'
					}],
					quotedPrice4: [{
						required: true,
						message: '请输入价格',
						trigger: 'blur,change'
					}],
					quotedPrice5: [{
						required: true,
						message: '请输入价格',
						trigger: 'blur,change'
					}],
					quotedPrice6: [{
						required: true,
						message: '请输入价格',
						trigger: 'blur,change'
					}],
					quotedPrice7: [{
						required: true,
						message: '请输入价格',
						trigger: 'blur,change'
					}],
					quotedPrice8: [{
						required: true,
						message: '请输入价格',
						trigger: 'blur,change'
					}],
				},
				//外贸校验
				foreignRules: {
					shippingCompany: [{
						required: true,
						message: '请输入船公司',
						trigger: 'blur,change'
					}],
					shippingFee: [{
						required: true,
						message: '请输入海运费',
						trigger: 'blur,change'
					}],
					portCharges: [{
						required: true,
						message: '请输入港杂费',
						trigger: 'blur,change'
					}],
					shippingDateETD: [{
						required: true,
						message: '请输入船期_ETD',
						trigger: 'blur,change'
					}],
					scheduleEta: [{
						required: true,
						message: '请输入船期_ETA',
						trigger: 'blur,change'
					}],
					departurePortBoxAngel: [{
						required: true,
						message: '请输入起运港(箱使/天)',
						trigger: 'blur,change'
					}],
					destinationPortBoxAngel: [{
						required: true,
						message: '请输入目的港(箱使/天)',
						trigger: 'blur,change'
					}],
					isItDirect: [{
						required: true,
						message: '请输入是否直达',
						trigger: 'blur,change'
					}],
				},
				workflowId: '1',
				type: "add",
				id: '', //外贸提交时用的id
				foreignTradeId: '', //外贸报价行项目id
			}
		},
		components: {},
		watch: {},
		onLoad(option) {
			this.form = {
				quotedPrice: '', //报价
				quoteDescription: '', //报价说明
				remark: '', //备注
				quotedPrice1: '', //≤1吨
				quotedPrice2: '', //≤3吨
				quotedPrice3: '', //≤5吨
				quotedPrice4: '', //≤10吨
				quotedPrice5: '', //≤15吨
				quotedPrice6: '', //≤20吨
				quotedPrice7: '', //≤25吨
				quotedPrice8: '', //≤30吨
				id: '', //内贸提交时的id
				shippingCompany: '', //船公司
				shippingFee: '', //海运费
				portCharges: '', //港杂费
				shippingDateETD: '', //船期_ETD
				scheduleEta: '', //船期_ETA
				departurePortBoxAngel: '', //起运港(箱使/天)
				destinationPortBoxAngel: '', //目的港(箱使/天)
				isItDirect: '', //是否直达
			}
			this.workflowId = option.workflowId
			this.type = option.type
			this.id = option.id
			if (option.foreignTradeId) {
				this.foreignTradeId = option.foreignTradeId
			} else {
				this.foreignTradeId = option.id
			}
			if (option.type === 'edit') {
				uni.setNavigationBarTitle({
					title: "修改报价"
				})
				if (option.workflowId === '3') {
					this.$u.api.carrier.getPriceDetail({
						id: option.foreignTradeId,
					}).then(data => {
						uni.hideLoading()
						if (data.code === 200 && data.success) {
							this.form = {
								...this.form,
								...data.data
							}
						}
					}).catch(() => {
						uni.hideLoading()
					})
				}

			}
			if (option.workflowId !== '3') {
				uni.showLoading({
					title: '加载中'
				})
				this.$u.api.carrier.getNewInquiryQuoted({
					id: option.id,
				}).then(data => {
					uni.hideLoading()
					if (data.code === 200 && data.success) {
						this.form = {
							...this.form,
							...data.data
						}
					}
				}).catch(() => {
					uni.hideLoading()
				})
			}

		},
		onReady() {
			if (this.workflowId === '1') {
				this.$refs.uForm.setRules(this.rules);
			} else if (this.workflowId === '2') {
				this.$refs.uForm.setRules(this.bulkCargoRules);
			} else {
				this.$refs.uForm.setRules(this.foreignRules);
			}
		},
		computed: {

		},
		methods: {
			//提交
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						uni.showLoading({
							title: '加载中'
						})
						this.$u.api.carrier.newQuotedPrice({
								...this.form
							}, this.workflowId === '3' ? this.foreignTradeId : this.form.id, this
							.workflowId === '3' ? this.id : this.form.id).then(data => {
							uni.hideLoading()
							if (data.code === 200 && data.success) {
								this.$refs.uToast.show({
									title: data.msg,
									type: 'success',
									duration: 1000,
									callback: (() => {
										this.back()
									})
								})
							}
						}).catch(() => {
							uni.hideLoading()
						})
					}
				})
			},
			//返回
			back() {
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style lang="less">
	.card-body {
		.u-form-item {
			padding: 20rpx !important;
		}

		.materials-title {
			color: #333333;
			font-size: 38rpx;
			line-height: 1.5;
			font-weight: 500;
			padding: 20rpx 0;
			width: 610rpx;
			margin: 0 auto;
			border-bottom: 2rpx solid #f2f3f6;
		}

		.title {
			color: #333333;
			font-size: 38rpx;
			font-weight: 500;
			padding: 30rpx 20rpx;
			width: 100%;
			border-bottom: 2rpx solid #f2f3f6;
		}
	}

	.u-footer {
		display: flex;
		justify-content: space-between;
	}
</style>
